package com.example2016.cjj.tuyademo;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";


    private Button btn_recover;
    private Button btn_paintcolor;
    private Button btn_paintsize;
    private Button btn_paintstyle;
    private SeekBar sb_size;


    private FrameLayout frameLayout;
    private TuYaView tuyaView;//自定义涂鸦板
    private int flWidth;
    private int flHeight;

    private Button btn_save;
    private Button btn_clear;
    private Button btn_undo;
    private Button btn_redo;
    private Button btn_select_paintcolor;
    private Button btn_select_paintstyle;
    private SeekBar seekBar;


    //颜色集合
    private int[] paintColor = new int[]{
            Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.BLACK, Color.GRAY, Color.CYAN
    };

    //记录上一次选中的画笔颜色值
    private int select_paint_color_index = 0;

    //记录上一次选中的画笔stylez值
    private int select_paint_style_index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();

        initListener();
    }
    private void initView() {
        frameLayout = (FrameLayout) findViewById(R.id.fl_content);

        frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                flWidth = frameLayout.getMeasuredWidth();
                flHeight = frameLayout.getMeasuredHeight();
                addTuYaView();

                frameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });


        btn_save = (Button) findViewById(R.id.btn_save);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_undo = (Button) findViewById(R.id.btn_undo);
        btn_redo = (Button) findViewById(R.id.btn_redo);
        btn_select_paintcolor = (Button) findViewById(R.id.btn_select_paintcolor);
        btn_select_paintstyle = (Button) findViewById(R.id.btn_select_paintstyle);
        seekBar = (SeekBar) findViewById(R.id.seekbar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tuyaView.selectPaintSize( progress );
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
    private void initData() {

    }


    private void  addTuYaView(){
        tuyaView = new TuYaView(this,flWidth,flHeight);
        frameLayout.addView(tuyaView);
        tuyaView.requestFocus();
    }


    private void initListener() {
        btn_save.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_undo.setOnClickListener(this);
        btn_redo.setOnClickListener(this);
        btn_select_paintcolor.setOnClickListener(this);
        btn_select_paintstyle.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_save:
                //保存
                tuyaView.saveToSDCard();
            break;
            case R.id.btn_clear:
                //清除
                tuyaView.cleardo();
                break;
            case R.id.btn_undo:
                //撤销
                tuyaView.undo();
                break;
            case R.id.btn_redo:
                //恢复
                tuyaView.redo();
                break;
            case R.id.btn_select_paintcolor:
                //画笔颜色
                showPaintColorDialog();
                break;
            case R.id.btn_select_paintstyle:
                //画笔样式,(画笔或橡皮擦)
                showPainStyleDialog();
                break;

        }
    }



    //画笔颜色对话框
    public void showPaintColorDialog(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("选择画笔颜色：");
        alertDialogBuilder.setSingleChoiceItems(R.array.paintcolor, select_paint_color_index, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                select_paint_color_index = which;
                tuyaView.selectPaintColor( paintColor[which] );
                dialog.dismiss();
            }
        });
        alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialogBuilder.create().show();
    }


    //弹出选择画笔或橡皮擦的对话框
    public void showPainStyleDialog(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("选择画笔或橡皮擦：");
        alertDialogBuilder.setSingleChoiceItems(R.array.paintstyle, select_paint_style_index, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                select_paint_style_index = which;
                tuyaView.selectPaintStyle(which);
                dialog.dismiss();
            }
        });
        alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialogBuilder.create().show();
    }


}
